package com.ukao.cashregister.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.eventbus.PayAmountEvent;
import com.ukao.cashregister.eventbus.PayEvent;
import com.ukao.cashregister.eventbus.UserInfoEvent;
import com.ukao.cashregister.listener.MyTextWatcher;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.widget.ClearEditText;
import com.ukao.cashregister.widget.StateImageView;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 *
 * Created by ukaodev on 2017/6/2.
 */

public class BalanceDialogFragment extends BaseDialogFragment {


    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.edit_name_et)
    ClearEditText editNameEt;
    @BindView(R.id.edit_name_cancel)
    Button editNameCancel;
    @BindView(R.id.edit_name_submit)
    Button editNameSubmit;
    private String balance;
    private Unbinder unbinder;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        balance = getArguments().getString(ARG_PARAM1);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_gathering_num, container, false);
        unbinder= ButterKnife.bind(this, view);
        initListener();
        return view;
    }


    @Override
    protected void initListener() {
        super.initListener();
        title.setText("零散充值");
        editNameEt.addTextChangedListener(mTextWatcher);
    }

    TextWatcher mTextWatcher = new MyTextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            super.onTextChanged(s, start, before, count);
            String str = s.toString();
            if (!CheckUtils.isEmpty(str)) {
                Constant.calculate(str, editNameEt);
            }
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }



    @Override
    protected boolean useEventBus() {
        return false;
    }

    @Subscribe
    public void onEvent(UserInfoEvent event) {

    }

    @OnClick({ R.id.edit_name_cancel, R.id.edit_name_submit,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case   R.id.close_btn:

                dismiss();

                break;

            case R.id.edit_name_cancel:

                dismiss();
                break;
            case R.id.edit_name_submit:
                String temMessage = editNameEt.getText().toString().trim();
                if (temMessage.isEmpty()) return;
                PayEvent.postHasData(PayEvent.Message.recharge, temMessage);
                dismiss();
                break;
        }
    }

    @Override
    public void onPause() {
        super.onPause();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
